<?php
    // Quy trinh xu ly
    /*
        1. Nhan Mo va Luu Mo
        2. Gui Mo sang Mecorp
        3. 
    */        
  date_default_timezone_set("Asia/Saigon");
  require_once("config.php");     
  require_once("functions.php");     

  // Get Tham So
  $mo = isset($_GET['mo'])?$_GET['mo'] :"";
  $serviceNumber = isset($_GET['serviceNumber'])?$_GET['serviceNumber'] :"";
  $mobile = isset($_GET['mobile'])?$_GET['mobile'] :"";
  $requestId = isset($_GET['requestId'])?$_GET['requestId'] :"";
  
  // Send Mo To MeCorp
   if(empty($mo)||empty($serviceNumber)||empty($mobile)||empty($requestId)){
       echo  0;
   }else{
           $mo = trim($mo);
           $mo = preg_replace("/(\s+)/si", " ", $mo);
           $arrMo = explode(" ", $mo);
           $cmdCode= $arrMo[0];
           $dateIime =date('Y-m-d H:i:s');
           $refcode = $arrMo[count($arrMo)-1];
                 
           $data = array();    
           $data["MOId"] = $requestId; 
           $data["Telco"] = checkTelcoMeCorp($mobile);
           $data["ServiceNum"] =$serviceNumber; 
           $data["Phone"] =$mobile;                      
           $data["Syntax"] =$cmdCode;                      
           $data["Message"] =$mo;                      
           $data["DateTime"] =$dateIime;
     
           $telco = checkTelcoMeCorp($mobile);
           $result =  sendMOMeCorp($requestId,$telco,$serviceNumber,$mobile,$cmdCode,$mo);
           
        // Luu log SMS
             $dataLog = array();
             $dataLog["transactionId"] = $requestId;
             $dataLog["mobile"] = $mobile;
             $dataLog["partnerId"] = 55; // MeCorp
             
             $userrow  = getUserByRefCode($refcode);
             if(!empty($userrow)) 
             $dataLog["userId"] = $userrow["id"]; else $data["userId"] = 0;
             
             $dataLog["appId"] = "0"; // Se lay theo id cua game
             $dataLog["dataId"] = 0;
             $dataLog["mo"] = $mo;
             $dataLog["telco"]  =checkTelco($mobile);
             $dataLog["serviceNumber"] = $serviceNumber;
             $dataLog["price"] = priceServiceCode($serviceNumber);
             $dataLog["status"]  = 0;
             $dataLog["os_type"] = 0;
             $dataLog["action"] = "";
             $dataLog["percent_wapmaster"] = 0;
             $dataLog["percent_partner"] = 0;
             $dataLog["penalty_mt"] = 0;
             $dataLog["type"] = 1; // Game
             $dataLog["status_mo"] = $result; // trang thai tra mo
             $dataLog["status_send_mt"] = 0; // chua gui MT
             
             // Get percent revenue 
              $gameId = getGameIdByCMDCodeMeCord($cmdCode);
              $dataLog["appId"] = $gameId;
              
              $rowApp = getGameInfo($gameId);
              if(!empty($rowApp)){
                      $percent_wapmaster=$rowApp["percent_wapmaster"];$percent_partner=$rowApp["percent_partner"];
                      $dataLog["percent_wapmaster"] = $percent_wapmaster;
                      $dataLog["percent_partner"] = $percent_partner;
              }
              $mt= "";
              $dataLog["mt"] = $mt;
             
              if(checkTransactionExist($data["transactionId"] )==0)
                InsertSmsLog($dataLog);
             else{
                 UpdateSmsLog($dataLog);
              }
          
           
           // Luu log file
            $datalog = "requestId=".$requestId."|mo=".$mo."|"."serviceNumber=".$serviceNumber."|"."mobile=".$mobile."|"."mt=".$mt."|status_mo".$result;
            writeLog($datalog,"/logSmsMe.txt");  
            
            if(intval($result)==0)
                echo 1;
             else 
                echo 0;
        
   }
  
    
?>
